package com.aijk.web.admin.ueditor;

import com.aijk.base.entity.FileInfo;
import com.aijk.commons.file.FileServerFactory;
import com.baidu.ueditor.define.AppInfo;
import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.MultiState;
import com.baidu.ueditor.define.State;
import net.viservice.editor.ueditor.UeditorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * UeditorService实现 - Fastdfs
 */
@Component
public class UeditorServiceImpl implements UeditorService {

    private final Logger logger = LoggerFactory.getLogger(UeditorServiceImpl.class);

    @Override
    public net.viservice.editor.MultipartFile getMultipartFile(String filedName, HttpServletRequest request) {
        net.viservice.editor.MultipartFile resultFile = null;
        try {
            MultipartHttpServletRequest multipartHttpservletRequest = (MultipartHttpServletRequest) request;
            MultipartFile multipartFile = multipartHttpservletRequest.getFile(filedName);
            if (!multipartFile.isEmpty()) {
                resultFile = new net.viservice.editor.StandardMultipartFile(filedName, multipartFile.getInputStream(), multipartFile.getOriginalFilename(), multipartFile.getSize());
            }
        } catch (IOException e) {
            logger.error("上传文件失败！", e);
        }
        return resultFile;
    }

    @Override
    public State saveFileByInputStream(net.viservice.editor.MultipartFile multipartFile, long maxSize) {
        State state;
        try {
            if (multipartFile.getSize() > maxSize) {
                return new BaseState(false, AppInfo.MAX_SIZE);
            }

            String originalFileName = multipartFile.getOriginalFilename();
            FileInfo uploadResult = FileServerFactory.get().uploadFile(multipartFile.getBytes(), originalFileName);

            state = new BaseState(true);
            state.putInfo("size", uploadResult.getLength());
            state.putInfo("title", uploadResult.getFileName());

            state.putInfo("url", uploadResult.getUrl());

            // 把上传的文件信息记入数据库
            // ---自行处理---
            return state;

        } catch (IOException e) {
            logger.error("上传文件失败！", e);
        }
        return new BaseState(false, AppInfo.IO_ERROR);
    }

    @Override
    public State saveBinaryFile(byte[] data, String fileName) {

        FileInfo uploadResult = FileServerFactory.get().uploadFile(data, fileName);

        State state = new BaseState(true);
        state.putInfo("size", uploadResult.getLength());
        state.putInfo("title", uploadResult.getFileName());

        state.putInfo("url", uploadResult.getUrl());

        // 把上传的文件信息记入数据库
        // ---自行处理---
        return new BaseState(false, AppInfo.IO_ERROR);
    }

    @Override
    public State listFile(String[] allowFiles, int start, int pageSize) {
        // 把计入数据库中的文件信息读取出来，返回即可

        // 下面的代码，仅作示例
        State state = new MultiState(true);
        state.putInfo("start", start);
        state.putInfo("total", 0);
        return state;
    }

    // ---------以下两个注释的方法，是我项目中的处理方式，可供参考-------------//

    // @SuppressWarnings("unchecked")
    // @Override
    // public State listFile(String[] allowFiles, int start, int pageSize) {
    // Pager pager = new Pager();
    //
    // int pageNumber = start / pageSize + 1;
    // pager.setPageSize(pageSize);
    // pager.setPageNumber(pageNumber);
    //
    // //System.out.println("allowFiles:"+Arrays.toString(allowFiles));
    // //allowFiles:[png, jpg, jpeg, gif, bmp]
    //
    // Criteria criteria = new Criteria();
    // criteria.add(Restrictions.order("createDate", "desc"));
    //
    // State state = null;
    // try {
    // pager = dfsFileService.getPager(getTenantId(), criteria, pager);
    // state = this.getState((List<DfsFile>) pager.getList());
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    //
    // state.putInfo("start", start);
    // state.putInfo("total", pager.getTotalCount());
    //
    // return state;
    // }
    //
    // private State getState(List<DfsFile> files) {
    // MultiState state = new MultiState(true);
    // BaseState fileState = null;
    //
    // for (DfsFile dfsFile : files) {
    // fileState = new BaseState(true);
    // fileState.putInfo("url",
    // PropertiesUtil.getValue("applicationContext.properties",
    // "dfsFileAccessBasePath") + "/" + dfsFile.getLink());
    // state.addState(fileState);
    // }
    // return state;
    // }

}
